package org.tcx.common;

import java.util.Collection;

public class LogUtil {
	public static String arg(String namesStr, Object... values) {
		StringBuffer buffer = new StringBuffer();
		String[] names = namesStr.split(",");
		int length = Math.min(names.length, values.length);
		for (int i = 0; i < length; i++) {
			Object value = values[i];
			String valueStr;
			valueStr = toString(value);
			buffer.append("[");
			buffer.append(names[i]);
			buffer.append(":");
			buffer.append(valueStr);
			buffer.append("]");
		}
		return buffer.toString();
	}

	private static String toString(Object value) {
		String valueStr;
		if (null == value) {
			valueStr = "null";
		} else if (value instanceof Collection) {
			valueStr = collection2Str((Collection<?>) value);
		} else {
			valueStr = value.toString();
		}
		return valueStr;
	}

	private static String collection2Str(Collection<?> value) {
		String valueStr;
		StringBuffer valuesb = new StringBuffer();
		valuesb.append("[");
		int i = 0;
		for (Object valuei : (Collection<?>) value) {
			valuesb.append(arg("item" + (i++), toString(valuei)));
		}
		valuesb.append("]");
		valueStr = valuesb.toString();
		return valueStr;
	}
}
